<template>
    <el-form :model="loginForm" :rules="formRules" ref="loginForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
        <h3 class="title"><img src="@/assets/logo.png" class="logo-image"/></h3>
        <el-form-item prop="loginAccount">
            <el-input type="text" v-model="loginForm.loginAccount" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="loginPassword">
            <el-input type="password" v-model="loginForm.loginPassword" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
            <el-button type="primary" style="width:100%;" @click="submitLogin" :loading="logining">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import {requestLogin} from '@/api/user';
    import {AUTH_CONSTANTS} from '@/CONSTANTS';
    import auth from "@/mixins/auth";
    import bus from '@/common/eventbus';

    export default {
        computed: {},
        mixins: [auth],
        data() {
            return {
                logining: false,
                loginForm: {
                    loginAccount: 'stjdydayou@163.com',
                    loginPassword: '123456',
                },
                formRules: {
                    loginAccount: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    loginPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                }
            };
        },
        methods: {
            submitLogin() {
                let _this = this;
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.logining = true;
                        let loginData = {
                            loginAccount: this.loginForm.loginAccount,
                            loginPassword: this.loginForm.loginPassword
                        };
                        this.httpPostRequest("/login", loginData, function (data) {
                            _this.logining = false;
                            if (data.authorities === undefined || data.authorities.length <= 0) {
                                bus.$emit("message", "warning", "对不起，您的账号没有权限进入");
                                _this.setAuthorities([]);
                            } else {
                                _this.setOauthUser(data.oauthUser);
                                _this.setAuthorities(data.authorities);
                                _this.$router.push('/main');
                            }
                        }, function () {
                            _this.logining = false;
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }

</script>

<style scoped>
    .login-container {
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .login-container .title {
        margin: 0px auto 40px auto;
        text-align: center;
    }

    .login-container .title .logo-image {
        width: 50%;
        margin: 0 auto;
    }

    .login-container .is-auto-login {
        margin: 0px 0px 35px 0px;
    }
</style>